{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bit5d211478a4cf4a3d93a2c891f6a9207d",
   "display_name": "Python 3.7.7 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "'''\n",
    "列表推导式中的初始表达式可以是任何表达式，包括另一个列表推导式。\n",
    "\n",
    "这个例子有点复杂，3X4 的矩阵，转置成 4X3\n",
    "'''\n",
    "\n",
    "matrix = [\n",
    "    [1, 2, 3, 4],\n",
    "    [5, 6, 7, 8],\n",
    "    [9,10,11,12]\n",
    "]\n",
    "\n",
    "new = []\n",
    "for i in range(len(matrix[0])):\n",
    "    colm = []\n",
    "    for row in matrix:\n",
    "        colm.append(row[i])\n",
    "    new.append(colm)\n",
    "\n",
    "new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "new = []\n",
    "for i in range(len(matrix[0])):\n",
    "    colm = [row[i] for row in matrix]\n",
    "    new.append(colm)\n",
    "new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "[[row[i] for row in matrix] for i in range(4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "list(zip(*matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[(1, 4), (2, 5), (3, 6)]"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "'-->  zip()函数'\n",
    "x = [1, 2, 3]\n",
    "y = [4, 5, 6, 7, 8]\n",
    "zipped = zip(x, y)\n",
    "list(zipped)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[4, 5, 6]"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "x2, y2= zip(*zip(x, y))\n",
    "list(x2)\n",
    "list(y2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}